#include "view_time_1.h"
#include "view_time_1.inc"
#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "weather_ctrl.h"
#include "view_times.h"
#include "math.h"
#if defined(TIME_1_SKIP)
#else
void view_time_1_display(void)
{

    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
        disp_ctrl.com_data.i_temp[1] = battery_ctrl_get_level(battery_ctrl_get_percent());
        disp_ctrl.com_data.i_temp[2] = UIdatetime.date.month;
        disp_ctrl.com_data.i_temp[3] = UIdatetime.date.day;
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.hour;
        disp_ctrl.com_data.i_temp[5] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[6] = UIdatetime.time.second;
        disp_ctrl.com_data.i_temp[7] = if_time_hour_system_12();
        if (disp_ctrl.com_data.i_temp[7])
        {
            disp_ctrl.com_data.i_temp[7] += (disp_ctrl.com_data.i_temp[4] >= 12); // 1=am,2=pm
            disp_ctrl.com_data.i_temp[4] %= 12;
            if (disp_ctrl.com_data.i_temp[4] == 0)
            {
                disp_ctrl.com_data.i_temp[4] = 12;
            }
        }

        disp_ctrl.com_data.i_temp[9] = get_present_week();
        disp_ctrl.com_data.i_temp[10] = battery_ctrl_if_charging();
        weather_record_t weather = {0};
        weather_ctrl_get(0, &weather); //获取天气
        if (weather.date_index == 0)
        {
            disp_ctrl.com_data.i_temp[11] = 0;
        }
        else
        {
            disp_ctrl.com_data.i_temp[11] = 1;
        }
        disp_ctrl.com_data.i_temp[12] = weather_get_temperature_type();
        if (disp_ctrl.com_data.i_temp[12] != WEATHER_TEMPERATURE_TYPE_C)
        {
            weather.temperature_min = round(32 + weather.temperature_min * 1.8);
            weather.temperature_max = round(32 + weather.temperature_max * 1.8);
        }
        disp_ctrl.com_data.i_temp[13] = weather.type;
        disp_ctrl.com_data.i_temp[14] = weather.temperature_min;
        disp_ctrl.com_data.i_temp[15] = weather.temperature_max;
//#warning debug
//        disp_ctrl.com_data.i_temp[11] = 1;
//        disp_ctrl.com_data.i_temp[14] = -11;
//        disp_ctrl.com_data.i_temp[15] = -21;
        disp_ctrl.auto_refresh_time = 1000;
    }
    // base
    int x = 157;
    int y = 76;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_3123_dial_157_76);
    for (int i = 0; i < 240; i++)
    {
        disp_draw_point(i, 67, 0x39a0);
        disp_draw_point(i, 150, 0x39a0);
    }
    x = 92;
    y = 168;
    disp_draw_user_image(x, y, time_1_slash_82_168);

    x = 83;
    y = 10;
    // #warning debug
    //     disp_ctrl.com_data.i_temp[0] = 0;
    if (disp_ctrl.com_data.i_temp[0])
    {

        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_connect);
    }
    else
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_disconnect);
    }
    x = 133;
    y = 12;
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_battery_back);
    if (disp_ctrl.com_data.i_temp[10])
    {
        disp_draw_user_image_ignore_color(x + 9, y + 0, RGB565_COLOR_BLACK, bmp_battery_charging);
    }
    else
    { // 2 2 7 2 12 2
        // 4x8 0x9cd3
        uint16_t color = 0x9cd3;
        if (disp_ctrl.com_data.i_temp[1] == 1)
        {
            color = 0x9020;
        }
        x += 2;
        y += 2;
        for (int i = 0; i < disp_ctrl.com_data.i_temp[1]; i++)
        {
            disp_draw_rect_color(x, y, x + 4 - 1, y + 8 - 1, color);
            x += 5;
        }
    }
    // week 131 169
    x = 131;
    y = 169;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_3125_week_mon_131_169 + disp_ctrl.com_data.i_temp[9] - 1);
    // time_1_9x15_TAB
    // month day
    x = 73;
    y = 169;
    disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[2], 2, 9, time_1_9x15_TAB);
    x = 101;
    y = 169;
    disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[3], 2, 9, time_1_9x15_TAB);

    // hour min
    x = 15;
    y = 83;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, FILE_IMG_3000_time_0 + disp_ctrl.com_data.i_temp[4] / 10);
    x = 44;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, x, y, FILE_IMG_3000_time_0 + disp_ctrl.com_data.i_temp[4] % 10);
    x = 94;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_4, x, y, FILE_IMG_3000_time_0 + disp_ctrl.com_data.i_temp[5] / 10);
    x = 123;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_5, x, y, FILE_IMG_3000_time_0 + disp_ctrl.com_data.i_temp[5] % 10);

    x = 77;
    y = 98;
    // 8x8
    disp_draw_rect_color(x, y, x + 7, y + 7, 0xff02);
    y = 118;
    disp_draw_rect_color(x, y, x + 7, y + 7, 0xff02);

    x = 157 + 33;
    y = 76 + 33;
    disp_draw_img_rotate(gImage_second, 0, 25, (x << 16) | y, disp_ctrl.com_data.i_temp[6] * 6);

    if (disp_ctrl.com_data.i_temp[11] == 0)
    {
        // FILE_IMG_3100_noweather_71_37
        x = 71;
        y = 37;
        disp_draw_user_image(x, y, time_1_weather_none);
        x = 106;
        y = 47;
        disp_draw_rect_color(x, y, x + 9, y + 1, RGB565_COLOR_WHITE);
        x += 11;
        disp_draw_rect_color(x, y, x + 9, y + 1, RGB565_COLOR_WHITE);
    }
    else
    {
        uint8_t const *weather_icon = time_1_weather_none;
        switch (disp_ctrl.com_data.i_temp[13])
        {
        case WEATHER_TYPE_SUNSHINE: //晴天
            weather_icon = time_1_weather_sun;
            break;
        case WEATHER_TYPE_CLOUDY: //多云
            weather_icon = time_1_weather_cloudy;
            break;
        case WEATHER_TYPE_RAINY: //雨天
            weather_icon = time_1_weather_rain;
            break;
        case WEATHER_TYPE_SNOW: //雪
            weather_icon = time_1_weather_snow;
            break;
        case WEATHER_TYPE_HAZE: //雾霾
            weather_icon = time_1_weather_frog;
            break;
        case WEATHER_TYPE_PARTLY_CLOUDY:
            weather_icon = time_1_weather_partly_cloudy;
            break;
        }
        x = 71;
        y = 37;
        disp_draw_user_image(x, y, weather_icon);
        int min_temp = abs(disp_ctrl.com_data.i_temp[14]);
        int max_temp = abs(disp_ctrl.com_data.i_temp[15]);
        x = 106;
        y = 40;
        if (disp_ctrl.com_data.i_temp[14] < 0)
        {
            disp_draw_rect_color(x, y + 7, x + 9, y + 8, RGB565_COLOR_WHITE);
            x += 11;
        }
        x = disp_draw_user_image_num(x, y, min_temp, 11, time_1_9x15_TAB);
        disp_draw_rect_color(x, y + 8, x + 4, y + 10, RGB565_COLOR_WHITE);
        x += 6;
        if (disp_ctrl.com_data.i_temp[15] < 0)
        {
            disp_draw_rect_color(x, y + 7, x + 9, y + 8, RGB565_COLOR_WHITE);
            x += 11;
        }
        x = disp_draw_user_image_num(x, y, max_temp, 11, time_1_9x15_TAB);
        y = 38;
        if (disp_ctrl.com_data.i_temp[12] != WEATHER_TEMPERATURE_TYPE_C)
        {
            disp_draw_user_image(x, y, time_1_weather_type_f);
        }
        else
        {
            disp_draw_user_image(x, y, time_1_weather_type_c);
        }
    }
}
#endif
